﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_Default : System.Web.UI.Page
{
    protected LocalFunctions FUNC;

    protected void Page_Load(object sender, EventArgs e)
    {
        FUNC = ((Admin)this.Master).FUNC;
        // Get data from DB about page
        if (!IsPostBack)
        {
            GetPageData();
        }
    }

    private void GetPageData()
    {
        
        Dal.arParams.Clear();
        Dal.arParams.Add(3); // typeID - 3- FOOTER
        DataTable DT = Dal.ExecuteDBsp("Admin_GetPageByID", Dal.arParams);

        // If not null - fill fields
        if (DT != null)
            if (DT.Rows.Count > 0)
            {
                DataRow Row = DT.Rows[0];
                hfID.Value = Row["ID"].ToString();
                txtRightBlock.Text = Helper.ConvertStringFromSql(Row["Title"].ToString());
                txtLeftBlock.Text = Helper.ConvertStringFromSql(Row["Content"].ToString());
            }
    }

    protected override void OnPreRender(EventArgs e)
    {
        DataTable DTIcons = Dal.ExecuteDBsp("Admin_GetFooterIcons");

        dgIcons.DataSource = DTIcons;
        dgIcons.DataBind();

        base.OnPreRender(e);
    }

    protected void btnArticleSave_Click(object sender, EventArgs e)
    {
        Dal.arParams.Clear();
        Dal.arParams.Add(Convert.ToInt32(hfID.Value)); //Page ID
        Dal.arParams.Add(string.Empty);
        Dal.arParams.Add(string.Empty);
        Dal.arParams.Add(string.Empty);
        Dal.arParams.Add(Helper.ConvertStringToSql(txtRightBlock.Text)); //Title
        Dal.arParams.Add(Helper.ConvertStringToSql(txtLeftBlock.Text)); //Content
        Dal.ExecuteDBsp("Admin_UpdatePageByID", Dal.arParams,true);
    }


    protected void dgIcons_RowEditing(object sender, GridViewEditEventArgs e)
    {
        dgIcons.EditIndex = e.NewEditIndex;
    }

    protected void dgEmployees_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        dgIcons.EditIndex = -1;
    }

    protected void dgIcons_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow row = dgIcons.Rows[e.RowIndex];

        String sNavigateURL = ((TextBox)row.FindControl("txtNavigateURL")).Text;

        Int32 iID = Convert.ToInt32(((HiddenField)row.FindControl("hfID")).Value);

        FileUpload fUploadIcon = ((FileUpload)row.FindControl("uplIcon"));
        string sIcon = ((HiddenField)row.FindControl("hfIcon")).Value;

        if (fUploadIcon.HasFile)
        {
            string sPath = Server.MapPath("~/content/galery");
            sIcon = "Icon_" + fUploadIcon.FileName;
            fUploadIcon.SaveAs(sPath + "\\" + sIcon);
        }

        FileUpload fUploadIconHover = ((FileUpload)row.FindControl("uplIconHover"));
        string sIconHover = ((HiddenField)row.FindControl("hfIconHover")).Value;

        if (fUploadIconHover.HasFile)
        {
            string sPath = Server.MapPath("~/content/galery");
            sIconHover = "Icon_" + fUploadIconHover.FileName;
            fUploadIconHover.SaveAs(sPath + "\\" + sIconHover);
        }

        UpdateRecord(iID,sNavigateURL, sIcon, sIconHover);
        dgIcons.EditIndex = -1;
    }

    private void UpdateRecord(int iID, string sNavigateURL, string sIcon, string sIconHover)
    {
        Dal.arParams.Clear();
        Dal.arParams.Add(iID);
        Dal.arParams.Add(sNavigateURL);   //Image
        Dal.arParams.Add(sIcon);
        Dal.arParams.Add(sIconHover);

        Dal.ExecuteDBsp("Admin_UpdateFooterIcons", Dal.arParams, true);
    }

    protected void dgIcons_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        GridViewRow row = dgIcons.Rows[e.RowIndex];

        Int32 iID = Convert.ToInt32(((HiddenField)row.FindControl("hfID")).Value);
        Dal.arParams.Clear();
        Dal.arParams.Add(iID);
        Dal.ExecuteDBsp("Admin_DeleteFooterIcon", Dal.arParams);
    }

    protected void btnFooterSave_Click(object sender, EventArgs e)
    {
        GridViewRow row = dgIcons.FooterRow;
       
        String sNavigateURL = string.Empty;
        Int32 iID = 0;
        String sIcon = string.Empty;
        String sIconHover = string.Empty;
        String sPath = Server.MapPath("~/content/galery");

        if (row != null)
        {
            sNavigateURL = ((TextBox)row.FindControl("txtNavigateURL")).Text;

            FileUpload fUploadIcon = ((FileUpload)row.FindControl("uplIcon"));
            
            if (fUploadIcon.HasFile)
            {
                sIcon = "Icon_" + fUploadIcon.FileName;
                fUploadIcon.SaveAs(sPath + "\\" + sIcon);
            }

            FileUpload fUploadIconHover = ((FileUpload)row.FindControl("uplIconHover"));

            if (fUploadIconHover.HasFile)
            {
                sIconHover = "IconHover_" + fUploadIconHover.FileName;
                fUploadIconHover.SaveAs(sPath + "\\" + sIconHover);
            }
        }
        else
        {
            GridViewRow gvr = (GridViewRow)dgIcons.Controls[0].Controls[0];
            TableCell cell = gvr.Cells[0];

            sNavigateURL = ((TextBox)cell.FindControl("txtNavigateURL")).Text;

            FileUpload fUploadIcon = ((FileUpload)cell.FindControl("uplIcon"));

            if (fUploadIcon.HasFile)
            {
                sIcon = "Icon_" + fUploadIcon.FileName;
                fUploadIcon.SaveAs(sPath + "\\" + sIcon);
            }

            FileUpload fUploadIconHover = ((FileUpload)cell.FindControl("uplIconHover"));

            if (fUploadIconHover.HasFile)
            {
                sIconHover = "IconHover_" + fUploadIconHover.FileName;
                fUploadIconHover.SaveAs(sPath + "\\" + sIconHover);
            }
        }

        UpdateRecord(iID, sNavigateURL, sIcon, sIconHover);
    }
}